package com.example.mvplogin.database;

import android.content.Context;

import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;

//指定类和版本号
@Database(entities = Student.class, version = 1)
public abstract class StudentDatabase extends RoomDatabase {

    //返回Dao
    public abstract StudentDao getStudentDao();

    static RoomDatabase studentDatabase;

    //Application的时候初始化
    public RoomDatabase getRoomDatabase(Context context) {
        if (studentDatabase == null) {
            synchronized (StudentDatabase.class) {
                if (studentDatabase == null) {
                    studentDatabase = Room.databaseBuilder(context,
                            StudentDatabase.class, "student-database").allowMainThreadQueries().build();
                }
            }
        }
        return studentDatabase;
    }

    //提供给外部的数据库对象,需要初始化才能使用y
    public RoomDatabase getRoomDatabase() {
        return studentDatabase;
    }

}
